home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / netaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-01  |  5.0 KB  |  281 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)$Id: netaux.c,v 1.16 1994/11/01 06:08:21 sob Exp sob $";
  3. #endif
  4.  
  5. /*
  6.  * Routines to deal with network stuff for
  7.  * stand-alone version of server.
  8.  */
  9.  
  10. #include "common.h"
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #ifndef EXCELAN
  14. #include <netdb.h>
  15. #endif
  16. #include <sys/ioctl.h>
  17. #include <signal.h>
  18. #ifdef USG
  19. #include <time.h>
  20. #else
  21. #include <sys/time.h>
  22. #endif
  23.  
  24. /*
  25.  * read_again -- (maybe) read in the active file again,
  26.  *    if it's changed since the last time we checked.
  27.  *
  28.  *    Parameters:    None.
  29.  *
  30.  *    Returns:    Nothing.
  31.  *
  32.  *    Side effects:    May change "num_groups" and "group_array".
  33.  */
  34.  
  35. void read_again()
  36. {
  37.     static long    last_mtime;    /* Last time active file was changed */
  38.     struct stat    statbuf;
  39.  
  40.     if (stat(activefile, &statbuf) < 0) {
  41. #ifdef SYSLOG
  42.         syslog(LOG_ERR, "read_again: can't stat active file: %m");
  43. #endif
  44.         return;
  45.     }
  46.  
  47.     if (statbuf.st_mtime != last_mtime) {
  48.         last_mtime = statbuf.st_mtime;
  49.         num_groups = read_groups();
  50.     }
  51. }
  52.  
  53. #ifdef ALONE
  54.  
  55. /*
  56.  * disassociate this process from the invoker's terminal.
  57.  * Close all file descriptors, and then open 0, 1, and 2 to
  58.  * somewhere bogus (i.e., "/", O_RDONLY).  This way we will know
  59.  * that stdin/out/err will at least be claimed.
  60.  *
  61.  *    Parameters:    None.
  62.  *
  63.  *    Returns:    Nothing.
  64.  *
  65.  *    Side effects:    Disassociates this process from
  66.  *            a terminal; closes file descriptors;
  67.  *            fd 0-2 opened as O_RDONLY to /.
  68.  */
  69.  
  70. disassoc()
  71. {
  72.     register int    i;
  73.  
  74. #ifdef USG
  75.     (void) signal(SIGTERM, SIG_IGN);
  76.     (void) signal(SIGINT, SIG_IGN);
  77.     (void) signal(SIGQUIT, SIG_IGN);
  78. #endif
  79.  
  80.     if (fork())
  81.         exit(0);
  82.  
  83.     for (i = 0; i < 10; i++)
  84.         (void) close(i);
  85.  
  86. #ifdef USG
  87.     (void) open("/", 0);
  88.     (void) dup2(0, 1);
  89.     (void) dup2(0, 2);
  90.     setpgrp();
  91.     umask(000);
  92. #else /* not USG */
  93.     i = open("/dev/tty", O_RDWR);
  94.     if (i >= 0) {
  95.         ioctl(i, TIOCNOTTY, 0);
  96.         (void) close(i);
  97.     }
  98.  
  99.     i = open("/", O_RDONLY);
  100.     if (i >= 0) {
  101.         if (i != 0) {            /* should never happen */
  102.             (void) dup2(i, 0);
  103.             (void) close(i);
  104.         }
  105.         (void) dup2(0, 1);
  106.         (void) dup2(1, 2);
  107.     }
  108. #endif /* not USG */
  109. }
  110.  
  111.  
  112. /*
  113.  * get_socket -- create a socket bound to the appropriate
  114.  *    port number.
  115.  *
  116.  *    Parameters:    None.
  117.  *
  118.  *    Returns:    Socket bound to correct address.
  119.  *
  120.  *    Side effects:    None.
  121.  *
  122.  *    Errors:        Syslogd, cause abortion.
  123.  */
  124.  
  125. get_socket()
  126. {
  127.     int            s;
  128.     struct sockaddr_in    sin;
  129. #ifndef EXCELAN
  130.     struct servent        *sp;
  131.     int on = 1;
  132.  
  133.     sp = getservbyname("nntp", "tcp");
  134.     if (sp == NULL) {
  135. #ifdef SYSLOG
  136.         syslog(LOG_ERR, "get_socket: tcp/nntp, unknown service.");
  137. #endif
  138.         exit(1);
  139.     }
  140. #endif /* not EXCELAN */
  141.  
  142.     bzero((char *) &sin, sizeof (sin));
  143.     sin.sin_family = AF_INET;
  144.     sin.sin_addr.s_addr = htonl(INADDR_ANY);
  145. #ifndef EXCELAN
  146.     sin.sin_port = sp->s_port;
  147.  
  148.     s = socket(AF_INET, SOCK_STREAM, 0);
  149. #else /* EXCELAN */
  150.     sin.sin_port = htons(IPPORT_NNTP);
  151.     s = 3;        /* WTF??? */
  152.     s = socket(SOCK_STREAM, (struct sockproto *)0, &sin,
  153.         (SO_KEEPALIVE|SO_ACCEPTCONN));
  154. #endif /* EXCELAN */
  155.     if (s < 0) {
  156. #ifdef EXCELAN
  157.         sleep(5);
  158.         return (-1);
  159. #else /* not EXCELAN */
  160. #ifdef SYSLOG
  161.         syslog(LOG_ERR, "get_socket: socket: %m");
  162. #endif /* SYSLOG */
  163.         exit(1);
  164. #endif /* not EXCELAN */
  165.     }
  166. #ifndef EXCELAN
  167. #ifdef SO_REUSEADDR
  168.     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  169.         (char *) &on, sizeof (on)) < 0)
  170.         perror("setsockopt - SO_REUSEADDR");
  171. #endif /* SO_REUSEADDR */
  172.     if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  173. #ifdef SYSLOG
  174.         syslog(LOG_ERR, "get_socket: bind: %m");
  175. #endif
  176.         exit(1);
  177.     }
  178. #endif /* not EXCELAN */
  179.  
  180.     return (s);
  181. }
  182.  
  183. /*
  184.  * make_stdio -- make a given socket be our standard input
  185.  *    and output.
  186.  *
  187.  *    Parameters:    "sockt" is the socket we want to
  188.  *            be file descriptors 0, 1, and 2.
  189.  *
  190.  *    Returns:    Nothing.
  191.  *
  192.  *    Side effects:    None.
  193.  */
  194.  
  195. make_stdio(sockt)
  196.     int    sockt;
  197. {
  198.     if (sockt != 0) {
  199.         (void) dup2(sockt, 0);
  200.         (void) close(sockt);
  201.     }
  202.     (void) dup2(0, 1);
  203.     (void) dup2(1, 2);
  204. }
  205.  
  206. #if 0
  207. /*
  208.  * set_timer -- set up the interval timer so that
  209.  *    the active file is read in every so often.
  210.  *
  211.  *    Parameters:    None.
  212.  *
  213.  *    Returns:    Nothing.
  214.  *
  215.  *    Side effects:    Sets interval timer to READINTVL seconds.
  216.  *            Sets SIGALRM to call read_again.
  217.  */
  218.  
  219. set_timer()
  220. {
  221. #ifndef USG
  222.     struct itimerval    new, old;
  223. #endif /* not USG */
  224.     extern int        read_again();
  225.  
  226.     (void) signal(SIGALRM, read_again);
  227. #ifdef USG
  228.     alarm(READINTVL);
  229. #else /* not USG */
  230.  
  231.     new.it_value.tv_sec = READINTVL;
  232.     new.it_value.tv_usec = 0;
  233.     new.it_interval.tv_sec = READINTVL;
  234.     new.it_interval.tv_usec = 0;
  235.     old.it_value.tv_sec = 0;
  236.     old.it_value.tv_usec = 0;
  237.     old.it_interval.tv_sec = 0;
  238.     old.it_interval.tv_usec = 0;
  239.  
  240.     if (setitimer(ITIMER_REAL, &new, &old) < 0) {
  241. #ifdef SYSLOG
  242.         syslog(LOG_ERR, "set_timer: setitimer: %m\n");
  243. #endif
  244.         exit(1);
  245.     }
  246. #endif /* not USG */
  247. }
  248. #endif /* 0 */
  249.  
  250.  
  251. /*
  252.  * reaper -- reap children who are ready to die.
  253.  *    Called by signal.
  254.  *
  255.  *    Parameters:    None.
  256.  *
  257.  *    Returns:    Nothing.
  258.  *
  259.  *    Side effects:    None.
  260.  */
  261.  
  262. reaper()
  263. {
  264. #ifndef USG
  265.     union wait    status;
  266.  
  267.     while (wait3(&status, WNOHANG, (struct rusage *)0) > 0)
  268.         ;
  269. #endif
  270. }
  271.  
  272. #else /* !ALONE */
  273.  
  274. /* Kludge for greenhill's C compiler */
  275.  
  276. static
  277. netaux_greenkludge()
  278. {
  279. }
  280. #endif /* not ALONE */
  281.